//-------------------------------------------------------------------
//	Copyright (c) 2012-2014 Zhirnov Andrey
//	This file is part of the "UXGEN-ES" project.
//	See copyright notice in "Engine.h".
//-------------------------------------------------------------------

#pragma once

#include "Base/Events/Event.h"
#include "Base/Include/BaseObject.h"

namespace Engine
{
namespace Base
{

	SHARED_POINTER( EventSystem );



	//
	// Event System
	//

	class EventSystem : public BaseObject
	{
	// types
	public:
		typedef delegate< void (const SysEvent &) >		EventCallback_t;

	private:
		typedef event< EventCallback_t >								event_listeners_t;
		typedef static_array< event_listeners_t, ESystemEvent::_COUNT >	event_list_t;


	// variables
	protected:
		event_list_t	_eventListeners;


	// methods
	public:
		EventSystem (const EngineSubSystemsRef ess) : BaseObject(ess)
		{
			ESS()->SetEventSystem( this );
		}


		~EventSystem ()
		{
			ESS()->SetEventSystem( null );
		}


		void Send (const SysEvent &ev) const
		{
			_eventListeners[ ev.type ].Call( ev );
		}


		virtual void Clear ()
		{
			_eventListeners.Clear();
		}


		virtual void Wait ()
		{
		}


		virtual void AttachEventListener (const EventCallback_t &callback, ESystemEvent::type eventType)
		{
			_eventListeners[eventType].Add( callback );
		}


		virtual void DetachEventListener (const EventCallback_t &callback, ESystemEvent::type eventType)
		{
			_eventListeners[eventType].Remove( callback );
		}


		static EventSystemPtr	New (const EngineSubSystemsRef ess)
		{
			return BaseObject::_New( new EventSystem( ess ) );
		}
	};


}	// Base
}	// Engine
